package com.withoutwords.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;

import com.withoutwords.R;
import com.withoutwords.engine.BitmapCache;
import com.withoutwords.engine.Game;
import com.withoutwords.engine.MyInfo;
import com.withoutwords.engine.settings.Settings;
import com.withoutwords.engine.social.SocialEngine;
import com.withoutwords.engine.social.SocialUser;
import com.withoutwords.ui.custom.CustomCheckedTextView;
import com.withoutwords.ui.custom.MyCustomTextView;



public class SettingsActvity extends BaseActivity {
	public final static int ResultLoggedOut = -200;
	public final static int RequestCode = 100;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings_layout);
		playSound(soundResId);
		
		final Settings sett = Settings.getInstance();
		int soundVolume  = sett.getSoundVolume();
		SeekBar soundbar = (SeekBar)findViewById(R.id.seekBarSound);
		soundbar.setMax(100);
		soundbar.setProgress(soundVolume);
		
		
		setFacebookName();
		
		Button btnLogout = (Button)findViewById(R.id.btnLogout);
		btnLogout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				SocialEngine.getSocialEngine().logout(SettingsActvity.this);
				sett.setMyInfo(new MyInfo(), true);
				setResult(ResultLoggedOut);
				Settings.getInstance().saveOpenedGames(new ArrayList<Game>());
				finish();
			}
		});
		
		PackageInfo pInfo;
		try {
			pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
			String version = pInfo.versionName;
			MyCustomTextView tv = (MyCustomTextView)findViewById(R.id.txtViewVersion);
			tv.setText(version);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		

		Button btnAbout  = (Button)findViewById(R.id.btnAbout);
		btnAbout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String urlPath = "";
				urlPath = getString(R.string.about_url);
				Uri uri = Uri.parse(urlPath);
				if (uri != null)
					startActivity(new Intent(Intent.ACTION_VIEW, uri));
				
			}
		});
		
		
	}
	public void setFacebookName()
	{
		final Settings sett = Settings.getInstance();
		MyInfo myinfo = sett.getMyInfo();
		if(myinfo != null)
		{
			SocialUser sUser = myinfo.getSocialUser();
			if(sUser != null)
			{
		String name  = sUser.getName();
		MyCustomTextView tv = (MyCustomTextView)findViewById(R.id.txtViewFacebookName);
		tv.setText(name);
			}
	}
	}
	
	@Override
	protected void setBitmaps() {
		// TODO Auto-generated method stub
		super.setBitmaps();
		View v = findViewById(R.id.SettingsLayout);
		v.setBackgroundDrawable(BitmapCache.getCacheInstance().getDrawable(this,
				R.drawable.blackboard));
		v.setVisibility(View.VISIBLE);
	}

	@Override
	protected void unsetBitmaps() {
		// TODO Auto-generated method stub
		super.unsetBitmaps();
		View ly = (ViewGroup) findViewById(R.id.SettingsLayout);
		if (ly != null) {
			ly.setVisibility(View.GONE);
			ly.setBackgroundDrawable(null);
			releaseBitmaps();
		}
	}

	@Override
	protected void releaseBitmaps() {
		// TODO Auto-generated method stub
		super.releaseBitmaps();

		BitmapCache.getCacheInstance().deleteDrawable(R.drawable.blackboard);

	}
	@Override
	public void finish() {
		SeekBar soundbar = (SeekBar)findViewById(R.id.seekBarSound);
		Settings.getInstance().setSoundVolume(soundbar.getProgress());
		super.finish();
	}
}
